package com.qf.cx.simplenms.link.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.qf.cx.simplenms.link.rai.model.RaiLimit;

public class TagResultSpec {

	/** logger. */
	private static final Logger LOG = Logger.getLogger(TagResultSpec.class);
	private static final int maxOpNum = RaiLimit.MAX_OP_NUM;
	
	private byte antennaId;
	private long seenTimeUTC;
	private byte[] tidData = new byte[8];
	
	/**记录底层上报的标签操作结果*/
	private List<AbstractOpResultSpec>  opResult = new ArrayList<>();
	
	public TagResultSpec() {
		super();
		// TODO Auto-generated constructor stub
	}

	public byte getAntennaId() {
		return antennaId;
	}

	public void setAntennaId(byte antennaId) {
		this.antennaId = antennaId;
	}

	public byte[] getTidData() {
		return tidData;
	}

	public void setTidData(byte[] tidData) {
		
		if(this.tidData.length != tidData.length)
		{
			LOG.error("data len error");
		}
		/**将底层struct的buffer数据拷贝到上层的buffer*/
		System.arraycopy(tidData, 0,this.tidData, 0, this.tidData.length);		
	}

	public void setTidData(List<Integer> tidData) {
		
		if(this.tidData.length != tidData.size())
		{
			LOG.error("data len error");
		}
		for(int i = 0; i < tidData.size(); i++){
			this.tidData[i] = tidData.get(i).byteValue();
		}	
	}	
	
	public long getSeenTimeUTC() {
		return seenTimeUTC;
	}

	public void setSeenTimeUTC(long seenTimeUTC) {
		this.seenTimeUTC = seenTimeUTC;
	}

	public List<AbstractOpResultSpec> getOpResult() {
		return opResult;
	}
	
	/*
	 * 添加op result
	 */
	public boolean addOpResult(AbstractOpResultSpec result)
	{
		if(this.opResult.size() >= maxOpNum)
		{
			LOG.error("op result list is full! max size is " + maxOpNum);
			return false;
		}
		
		this.opResult.add(result);

		//LOG.debug("add op result spec success!");
		
		return true;
		
	}
	
	public void deleteAllOpResult()
	{
		this.opResult.clear();
	}
	
	private int[] byte2uchar(byte[] s)
	{
		int[] d = new int[s.length];
		int tmp = 0;
		for(int i = 0; i < s.length; i++)
		{
			tmp  = s[i];
			tmp = tmp & 0xff;
			d[i] = tmp;
		}		
		return d;
	}
	@Override
	public String toString() {
		
		return "TagResultSpec [antennaId=" + antennaId + ", tidData=" + Arrays.toString(byte2uchar(tidData)) + ", opResult="
				+ opResult + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + antennaId;
		result = prime * result + ((opResult == null) ? 0 : opResult.hashCode());
		result = prime * result + Arrays.hashCode(tidData);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		TagResultSpec other = (TagResultSpec) obj;
		if (antennaId != other.antennaId) {
			return false;
		}
		if (opResult == null) {
			if (other.opResult != null) {
				return false;
			}
		} else if (!opResult.equals(other.opResult)) {
			return false;
		}
		if (!Arrays.equals(tidData, other.tidData)) {
			return false;
		}
		return true;
	}
	
	
	
	
}
